import { defineStore } from 'pinia'
import { ref } from 'vue'

// 用户模块 token setToken removeToken
export const useUserStore = defineStore(
  'big-user',
  () => {
    const token = ref('')
    const userRole = ref(null)

    const setToken = (newToken) => {
      token.value = newToken
      // localStorage.setItem('token', token)
    }
    const setUserRole = (newUserRole) => {
      userRole.value = newUserRole
      // console.log(userRole.value)
    }

    const removeToken = () => {
      token.value = ''
    }
    const removeUserRole = () => {
      userRole.value = null
    }

    const logout = () => {
      removeToken()
      removeUserRole()
    }

    return {
      token,
      setToken,
      userRole,
      removeToken,
      logout,
      setUserRole,
      removeUserRole
    }
  },
  {
    persist: {
      enabled: true,
      strategies: [
        {
          key: 'big-user',
          storage: localStorage,
          paths: ['token', 'userRole'] // 明确指定需要持久化的状态
        }
      ]
    }
  }
)
